package com.tjshop.android;

import com.tjshop.android.util.SharedPreferencesUtil;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;

public class DisPatchActivity extends BaseActivity implements
		View.OnClickListener {

	CheckBox selectShunfeng,selectYunda,selectYouZheng;
	RelativeLayout shunfeng,yunda,youzheng;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dispatching);
		
		setTheme(R.style.Theme_Sherlock);
		updateCommonReturnBar("配送方式",new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				complete();
			}
		});
		
		selectShunfeng = (CheckBox) findViewById(R.id.selectShunfeng);
		selectYunda = (CheckBox) findViewById(R.id.selectYunda);
		selectYouZheng = (CheckBox) findViewById(R.id.selectYouZheng);
		
		String str = SharedPreferencesUtil.get(getApplicationContext(), "config", "remember_peisong");
		if(str != null){
			if(str.equals("顺丰快递")){
				selectShunfeng.setChecked(true);
				selectYouZheng.setChecked(false);
				selectYunda.setChecked(false);
			}else if(str.equals("中国邮政")){
				selectShunfeng.setChecked(false);
				selectYouZheng.setChecked(true);
				selectYunda.setChecked(false);
			}else{
				selectShunfeng.setChecked(false);
				selectYouZheng.setChecked(false);
				selectYunda.setChecked(true);
			}
		}
		shunfeng = (RelativeLayout) findViewById(R.id.shunfeng);
		youzheng = (RelativeLayout) findViewById(R.id.youzheng);
		yunda = (RelativeLayout) findViewById(R.id.yunda);
		shunfeng.setOnClickListener(this);
		youzheng.setOnClickListener(this);
		yunda.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.shunfeng:
			selectShunfeng.setChecked(true);
			selectYouZheng.setChecked(false);
			selectYunda.setChecked(false);
			break;
		case R.id.youzheng:
			selectShunfeng.setChecked(false);
			selectYouZheng.setChecked(true);
			selectYunda.setChecked(false);
			break;
		case R.id.yunda:
			selectShunfeng.setChecked(false);
			selectYouZheng.setChecked(false);
			selectYunda.setChecked(true);
			break;
		default:
			break;
		}
	}
	private void complete() {
		String str = "顺丰快递";
		if(selectYouZheng.isChecked()){
			str = "中国邮政";
		}else if(selectYunda.isChecked()){
			str = "韵达快递";
		}
		
		Intent it = new Intent();  
        it.putExtra("result", str);  
		setResult(RESULT_OK, it);
		finish();
	}
	
	@Override
	public void onBackPressed() {
		complete();
		super.onBackPressed();
	}

	@Override
	public boolean handleMessage(Message arg0) {
		return false;
	}
}
